package accs.model.proxy;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import accs.model.Account;
import accs.model.AccountChecker;
import accs.model.Context;
import accs.model.ExtendedHttpClient;
import accs.model.utils.HeaderUtils;

public class CheckProxy implements AccountChecker {
	public CheckProxy() {
	}
	
	public Connection checkWhoer(String proxyhost, int port) throws ClientProtocolException, IOException{
        
        DefaultHttpClient httpclient=new ExtendedHttpClient(proxyhost,port);
        
        //System.setProperty("http.proxyHost",proxyhost);
        //System.setProperty("http.proxyPort",String.valueOf(port));  
        
		HttpGet get=new HttpGet("http://whoer.net/");
		get.setHeader(HeaderUtils.getUserAgent());
		HttpResponse resp=(httpclient).execute(get);
		BufferedReader br=new BufferedReader(new InputStreamReader(resp.getEntity().getContent(),"utf-8"));
		String line=null;
		
		
		int linenumber = 0;
		String ip = null;
		String country = null;
		int h = -5;
		int p = -5;
		int t = -5;
		int d = -5;
		int d2 = -6;
		int b = -7;
		boolean blacklist = false;
		String dns = null;
		String dnscountry = null;
		String host = null;
		boolean proxy = false;
		boolean browsermatch = false;
		boolean timematch = false;
		while((line=br.readLine())!=null) {
			line=line.trim(); //System.out.println(line);
			linenumber++;
			
			if(line.startsWith("<td colspan=\"3\" class=\"wtr\"><div id=\"remote_addr\" class=\"ipadotted\">")) { 	
				ip = line.replaceAll("<td colspan=\"3\" class=\"wtr\"><div id=\"remote_addr\" class=\"ipadotted\">", "").replaceAll("</div>&nbsp;</td>", "");
//				System.out.println(ip);
			}
			if(line.startsWith("<div class=\"ipadotted\">")){
				country = line.replaceAll("<div class=\"ipadotted\">", "").replaceAll("</div>", "");				
			}
			if(line.startsWith("<!-- Host -->")){
				h = linenumber+4;
			}
			if(h == linenumber){
				host = line.replaceAll("<span class=\"disabled\">", "").replaceAll("</span>", "");
			}
			if(line.startsWith("<!-- Proxy -->")){
				p = linenumber+4;
			}
			if(p == linenumber){
				if(line.contains("No")){
					proxy = false;
				}else{
					proxy = true;
				}
			}
			if(line.startsWith("<td rowspan=\"2\" class=\"wtr_time\"><div id=\"browser_diff\">")){
				if(line.contains("Mismatch")){
					browsermatch = false;
				}else{
					browsermatch = true;
				}
			}
			if(line.startsWith("<!-- Time local -->")){
				t = linenumber+4;
			}
			if(t == linenumber){
				if(line.contains("Mismatch")){
					timematch = false;
				}else{
					timematch = true;
				}
			}
			if(line.startsWith("<!-- DNS unique domain -->")){
				d = linenumber+3;
				d2 = linenumber+4;
			}
			if(d == linenumber){
				//System.out.println(line);
				if(line.contains("N/A")){
					dns = "N/A";
				}else{
				dns = line.replaceAll("<tbody><tr><td width=\"100\">", "").replaceAll("</td><td>", "");
				}
			}
			if(d2 == linenumber && dns != "N/A"){
				//System.out.println(line);
				dnscountry = line.substring(line.indexOf(">")+1);
			}
			if(line.startsWith("<!-- DSBL -->")){
				b = linenumber+6;
			}
			if(b == linenumber){
				if(line.contains("No")){
					blacklist = false;
				}else{
					blacklist = true;
				}
			}
		}
		
		Connection cc = new Connection(proxyhost, port , host, ip, country, proxy, timematch, browsermatch, dns, dnscountry, blacklist);
		return cc;
	}
	
	public String checkIfconfig(String proxyHost, int proxyPort) throws ClientProtocolException, IOException {
		ExtendedHttpClient client=new ExtendedHttpClient(proxyHost, proxyPort);
		
		HttpGet get=new HttpGet("http://ifconfig.me/ip");
		HttpResponse resp=client.execute(get);
		
		return EntityUtils.toString(resp.getEntity());
	}

	public Account login(String user, String password, Context ctx) throws Exception {
		return checkWhoer(user, Integer.parseInt(password));
	}

	public URL getImage() {
		return null;
	}

	public String getName() {
		return "proxy";
	}
}